﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dawn_of_Man
{
    public partial class EntryForm : Form
    {
        Tribe t = null;
        Controller c = null;
        public EntryForm(string[] args)
        {
            InitializeComponent();
            c = new Controller();
            t = c.dc.generateTribe();
            lbTribe.DataSource = t.tribe;
            cbAssignment.DataSource = c.dc.getAssignmentsList();
            StorageHut storageBuilding = t.village.storageHut;
            storageBuilding.Changed += new EventHandler(updateStorageDisplay);
            lbBuildings.DataSource = t.village.buildings;
            lbAreas.DataSource = t.village.areas;
            updateStorageDisplay(null, null);
            updateConstructionStats();
            cheatToolStripMenuItem.Enabled = false;
            cheatToolStripMenuItem.Visible = false;
            if (args.Length > 0)
                if (args[0] == "debug")
                {
                    cheatToolStripMenuItem.Enabled = true;
                    cheatToolStripMenuItem.Visible = true;
                }
        }

        private void updateStorageDisplay(object sender, EventArgs e)
        {
            try
            {
                StorageHut storageBuilding = t.village.storageHut;
                tbClay.Text = storageBuilding.clay.ToString();
                tbStone.Text = storageBuilding.stone.ToString();
                tbMeat.Text = storageBuilding.meat.ToString();
                tbHide.Text = storageBuilding.hide.ToString();
                tbWater.Text = storageBuilding.water.ToString();
                tbWood.Text = storageBuilding.wood.ToString();
                tbWheat.Text = storageBuilding.wheat.ToString();
                tbTools.Text = storageBuilding.tool.ToString();
                tbWeapons.Text = storageBuilding.weapon.ToString();
                tbBuildingBlocks.Text = storageBuilding.block.ToString();
            }
            catch (Exception error)
            {

                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void lbTribe_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lbTribe.Items.Count > 0 && lbTribe.SelectedItem != null)
                {
                    
                    Homosapien man = (Homosapien)lbTribe.SelectedItem;
                    tbName.Text = man.name;
                    tbAge.Text = man.age.ToString();
                    tbSex.Text = man.GetSex();
                    tbHealth.Text = man.GetHealth();
                    tbHunger.Text = man.GetHunger();
                    if (cbAssignment.Items.Count > 0)
                    {
                        cbAssignment.SelectedIndex = 0;
                        cbAssignment.SelectedItem = man.assignment;
                    }
                    if (man.age < 15)
                    {
                        cbAssignment.Enabled = false;
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void cbActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Assignment selectedAssignment = (Assignment)cbAssignment.SelectedItem;
                cbActivity.Visible = true;
                Homosapien selectedMan = (Homosapien)lbTribe.SelectedItem;
                switch (selectedAssignment.name)
                {
                    case "Gather":
                    case "Hunt":
                        cbActivity.DataSource = t.village.surroundingAreas();
                        cbActivity.SelectedItem = selectedMan.area;
                        break;
                    case "Craft":
                        cbActivity.DataSource = t.village.craftBuildings();
                        break;
                    case "Build":
                    case "Mate":
                    case "Nothing":
                        cbActivity.Visible = false;
                        break;
                    default:
                        cbActivity.DataSource = null;
                        break;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void lbBuildings_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Building building = (Building)lbBuildings.SelectedItem;
                updateBuildingStats(building);
            }
            catch (Exception error)
            {
                
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void btSetActivity_Click(object sender, EventArgs e)
        {
            try
            {
                Assignment assignment = (Assignment)cbAssignment.SelectedItem;
                Homosapien worker = (Homosapien)lbTribe.SelectedItem;
                if (cbActivity.Enabled && cbActivity.Visible)
                {
                    Area area = c.getAreaFromList(t, cbActivity.SelectedItem.ToString());
                    Building building = c.getBuildingFromList(t, cbActivity.SelectedItem.ToString());

                    c.addWorkerToActivity(building, area, assignment, worker);

                    updateAreaStats(area);
                    updateBuildingStats(building);
                }
                else
                    c.addWorkerToActivity(null, null, assignment, worker);
                    
                updateConstructionStats();
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }



        }

        private void updateBuildingStats(Building building)
        {
            if (building != null)
            {
                tbBuildingSize.Text = building.size.ToString();
                tbGoodsTurn.Text = building.baseGoodPrTurn.ToString();
                tbTotalGoodsTurn.Text = building.getTotalGoodsPrTurn().ToString();
                lbWorkers.DataSource = null;
                lbWorkers.DataSource = building.getWorkers();
            }
        }

        private void updateAreaStats(Area area)
        {
            if (area != null)
            {
                lbHunters.DataSource = null;
                lbHunters.DataSource = area.hunting;
                lbGatherers.DataSource = null;
                lbGatherers.DataSource = area.working;
            }
        }

        private void updateConstructionStats()
        {
            lbConstructors.DataSource = null;
            lbConstructors.DataSource = c.GetConstructors(t);
            lbConstructions.DataSource = null;
            lbConstructions.DataSource = c.GetConstructionOptions(t);
        }

        private void lbAreas_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Area area = (Area)lbAreas.SelectedItem;
                updateAreaStats(area);
            }
            catch (Exception error)
            {

                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void endTurn(object sender, EventArgs e)
        {
            try
            {
                c.EndTurn(t);
                lbTribe.DataSource = null;
                lbTribe.DataSource = t.tribe;
                lbTribe.SelectedIndex = 0;
                lbTurn.Text = t.turn.ToString();
            }
            catch (Exception error)
            {
                
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void Construction_Changed(object sender, EventArgs e)
        {
            Building b = (Building)lbConstructions.SelectedItem;
            if (b != null)
            {
                buildingProgress.Value = 100 - b.construction;
                tbConBlocks.Text = b.getConstructionBlocksPrTurn().ToString();
                tbConWood.Text = b.getConstructionWoodPrTurn().ToString();
                tbConTools.Text = b.getConstructionToolsPrTurn().ToString();
                int constructors = lbConstructors.Items.Count;
                if (constructors > 0)
                {
                    tbTurnsLeft.Text = b.getTurnsLeft(constructors).ToString();
                }
            }
        }

        private void btBuild_Click(object sender, EventArgs e)
        {
            try
            {
                Building b = (Building)lbConstructions.SelectedItem;
                if (b != null)
                {
                        MessageBox.Show(c.DoConstruction(t, b, c.GetConstructors(t)));
                        updateConstructionStats();
                }
            }
            catch (Exception error)
            {

                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void save(object sender, EventArgs e)
        {
            try
            {
                c.SaveGame(t);
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void load(object sender, EventArgs e)
        {
            try
            {
                Tribe newTribe = null;
                newTribe = c.LoadGame(@"C:\Arbejde\Dawn of Man\Dawn of Man\bin\Debug\DOM.sav");
                if (newTribe.village.name != null)
                {
                    MessageBox.Show(newTribe.village.name + " loaded successfull");
                    t = newTribe;
                    lbTribe.DataSource = null;
                    lbTribe.DataSource = t.tribe;
                    lbTribe.SelectedIndex = 0;
                    lbTurn.Text = t.turn.ToString();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }
        }

        private void resourceCheat(object sender, EventArgs e)
        {
            StorageHut sh = t.village.storageHut;
            if (sender == woodToolStripMenuItem)
                c.addRessource("Wood", 20, sh);
            else if (sender == clayToolStripMenuItem)
                c.addRessource("Clay", 20, sh);
            else if (sender == waterToolStripMenuItem)
                c.addRessource("Water", 20, sh);
            else if (sender == stoneToolStripMenuItem)
                c.addRessource("Stone", 20, sh);
            else if (sender == wheatToolStripMenuItem)
                c.addRessource("Wheat", 20, sh);
            else if (sender == meatToolStripMenuItem)
                c.addRessource("Meat", 20, sh);
            else if (sender == hideToolStripMenuItem)
                c.addRessource("Meat", 20, sh);
            else if (sender == toolsToolStripMenuItem)
                c.addRessource("Tool", 20, sh);
            else if (sender == weaponsToolStripMenuItem)
                c.addRessource("Weapon", 20, sh);
            else if (sender == buildingBlocksToolStripMenuItem)
                c.addRessource("Block", 20, sh);
        }



    }
}
